package timex

import "time"

func GetWeekStartTime(targetTime time.Time, startOfWeek time.Weekday, loc *time.Location) time.Time {
	deltaDayOfWeek := 0

	deltaDayOfWeek = int(targetTime.Weekday() - startOfWeek)
	if deltaDayOfWeek < 0 {
		deltaDayOfWeek += 7
	}

	dayStart := time.Date(targetTime.Year(), targetTime.Month(), targetTime.Day(), 0, 0, 0, 0, loc)
	weekStart := dayStart.Add(-time.Hour * 24 * time.Duration(deltaDayOfWeek))
	return weekStart
}

func GetTimeOfCurrentWeek(pivot time.Time, startOfWeek time.Weekday, weekDay time.Weekday, hour int8, loc *time.Location) time.Time {
	weekStart := GetWeekStartTime(pivot, startOfWeek, loc)
	deltaDay := weekDay - weekStart.Weekday()
	if deltaDay < 0 {
		deltaDay += 7
	}
	return weekStart.AddDate(0, 0, int(deltaDay)).Add(time.Duration(hour) * time.Hour)
}

func GetStartTimeOfOneDayWithLocation(t time.Time, local *time.Location) time.Time {
	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, local)
}
